package com.glc.hms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glc.hms.system.dao.SysRoleMenuMapper;
import com.glc.hms.system.entity.SysMenuaction;
import com.glc.hms.system.entity.SysRoleMenu;
import com.glc.hms.system.service.ISysMenuactionService;
import com.glc.hms.system.service.ISysRoleMenuService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色与菜单对应关系 服务实现类
 * </p>
 *
 * @author Santa
 * @since 2019-11-20
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper menuMapper;
    @Autowired
    private ISysMenuactionService menuactionService;


    /**
     * 根据roleId查询对应的菜单
     * @param roleId
     * @return
     */
    @Override
    public List<SysRoleMenu> queryMenuByRole(Long roleId){
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<SysRoleMenu> sysRoleMenus = menuMapper.selectList(queryWrapper);
        return sysRoleMenus;
    }


    /**
     * 获取该角色对应子菜单的操作权限
     * @param roleId
     * @param menuId
     * @return
     */
    public List<SysMenuaction> queryActionByRoleIdAndMenuId(Long roleId,Long menuId){
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId);
        queryWrapper.eq("role_id",roleId);
        List<SysRoleMenu> sysRoleMenus = menuMapper.selectList(queryWrapper);
        List<SysMenuaction> list = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(sysRoleMenus)){
            for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
                Integer actionId = sysRoleMenu.getActionId();
                SysMenuaction sysMenuaction = menuactionService.queryActionNameById(actionId, menuId);
                list.add(sysMenuaction);
            }
        }
        return list;
    }
}
